package com.terana.lbs.marketing.web.repository;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.dos.MarketingActivityDO;
import com.tarena.lbs.pojo.marketing.po.ActivityPO;
import com.tarena.lbs.pojo.marketing.query.ActivityQuery;
import com.terana.lbs.marketing.web.mapper.ActivityMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;
    public PageInfo<ActivityPO> activityPage(ActivityQuery query) {
        //构造一个查询条件
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",query.getBusinessId());
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<ActivityPO> activityPOS = activityMapper.selectList(queryWrapper);
        PageInfo<ActivityPO> activityPOPageInfo = PageInfo.of(activityPOS);
        return activityPOPageInfo;

    }

    public void save(ActivityPO activityPO) {
        activityMapper.insert(activityPO);
    }

    public ActivityPO getActivityById(Integer id) {
        return activityMapper.selectById(id);
    }

    public MarketingActivityBO getActivitiesById(Integer activityId) {
        MarketingActivityDO aDo=activityMapper.selectActivityById(activityId);
        MarketingActivityBO bo=new MarketingActivityBO();
        if (aDo!=null){
            BeanUtils.copyProperties(aDo,bo);
        }
        return bo;
    }

    public List<MarketingActivityBO> getActivityByShopId(Integer shopId) {
        List<MarketingActivityDO> dos = activityMapper.selectActivitiesById(shopId);
        if (CollectionUtils.isNotEmpty(dos)){
            return dos.stream().map(Do->{
                MarketingActivityBO bo = new MarketingActivityBO();
                BeanUtils.copyProperties(Do,bo);
                return bo;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
